// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
let login = require("./Login.js");
var Utils = require("./Utils.js");
var handy = require("./handy.js");

cc.Class({
    extends: cc.Component,

    properties: {
        avatarImgNode: {
            default: null,
            type: cc.Node
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
    },

    init() {
        this.login = new login();
        this.avatarNameNode = this.node.getChildByName("friend_name_layout").getChildByName("friend_name");
        this.avatarGenderNode = this.node.getChildByName("friend_name_layout").getChildByName("friend_gender");
        this.ageNode = this.node.getChildByName("friend_age");
        this.avatarBgNode = this.node.getChildByName("friend_avatar_bg");
        this.topTipNode = this.node.getChildByName("top_tip");
        this.topTipNode.active = false;

        this.avatarNameLabel = this.avatarNameNode.getComponent(cc.Label);
        this.avatarGenderSprite = this.avatarGenderNode.getComponent(cc.Sprite);
        this.ageLabel = this.ageNode.getComponent(cc.Label);
        this.avatarBgAni = this.avatarBgNode.getComponent(cc.Animation);
        this.matchSuccessNode = this.node.getChildByName("friend_match_success");
        if(this.matchSuccessNode) {
           this.matchSuccessAni = this.matchSuccessNode.getComponent(cc.Animation);
        }
        this.avatarBgAni.play("wait_friend");
    },


    onMatchSuccess(userInfo){
        console.log("onMatchSuccess, icon:" + userInfo.icon + " name = "+userInfo.name + " type ="+ userInfo.type);
        Utils.resetFrameFromNet(this.avatarImgNode.getComponent(cc.Sprite),userInfo.icon);

        this.avatarBgAni.stop("wait_friend");
        if(this.matchSuccessAni) {
            this.matchSuccessAni.play("match_success");
        }

        this.avatarNameLabel.string = userInfo.name;
        if(userInfo.gender == 2) {
            Utils.resetFrame(this.avatarGenderSprite,"mzGameResource/female");
        }else {
            Utils.resetFrame(this.avatarGenderSprite,"mzGameResource/male");
        }
        this.ageLabel.string = handy.getAge(userInfo.birth) + " " + handy.getAstroFromBirth(userInfo.birth);
    },

    showTopTip(content){
        this.topTipNode.active = true;
        this.topTipNode.getChildByName("tip_content").string = content;
    }
    // update (dt) {},
});
